<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
Object obj=request.getSession().getAttribute("role_resource");
if(null==obj){
	response.setStatus(1000);
	return;
}
%> 
    
	<script type="text/javascript">
			var thislimit=19;
			//搜索条件外包商下拉列表
			var epiboly_store1=new Ext.data.JsonStore({
				root: "data",
				fields : [ "id", "name"],
				url : "epiboly!listAll.action",
				listeners : {
					"load": function() {
						var RecordName = Ext.data.Record.create([
							{name: "id"},
							{name: "name"}
						]);
						var newRecord = new RecordName({
							id: "",
							name: "全部"
						});
						this.insert(0,newRecord);
					}
				}
			});
			//打印方式下拉列表
			print_store1=new Ext.data.ArrayStore({
				fields: [
					"id", "text"
				],
				data: [
					["", "全部"],["0", "滚筒"],["1","折叠"]
				]
			});
			//打印方式下拉列表
			print_store=new Ext.data.ArrayStore({
				fields: [
					"id", "text"
				],
				data: [
					["0", "滚筒"],["1","折叠"]
				]
			}),
			//外包商下拉列表
			epiboly_store=new Ext.data.JsonStore({
				root: "data",
	    		fields : [ "id", "name"],
				url : "epiboly!listAll.action"
			});
			//卡产品树数据源
			var tree_store=new Ext.data.JsonStore({
	    		root: "data",
	    		fields: ["id","text","leaf"],
	    		url: "product!ProdType.action"
	    	});
			//作业分配列表数据源
			var data_store=new Ext.data.JsonStore({
	    		root: "data",
	    		totalProperty: "total",
	    		fields: ["businpnt_no", "businpnt_name","city_no", "city_name", "card_no", "card_name", "print_type"],
	  			url:"epibCity!list.action"
	    	});

	    	var oper = function(url, params, parent, panel){
				Ext.Ajax.request({
					clientValidation: true,
					params: params,
					url: url,
					success: function(response, opts){			
						var d=eval("("+response.responseText+")");
						if(d.code == "1"){
							if(parent != null) {
								Ext.getCmp("p1").getBottomToolbar().doRefresh();
							}
							Ext.Msg.show({
		   						title:"提示",
		   						msg:"操作成功!",
		   						buttons:Ext.Msg.OK,
		   						icon: Ext.MessageBox.INFO
							});										
							if(panel != null)
								panel.layout.setActiveItem(0);
						} else {
							Ext.Msg.show({
		   						title:"提示",
		   						msg:"操作失败!",
		   						buttons:Ext.Msg.OK,
		   						icon: Ext.MessageBox.ERROR
							});
						}
						Ext.getCmp("save").setDisabled(false);
					}
				});
	    	};

	    	var sel = function(){
		    	var node = Ext.getCmp("epibCityTree").getSelectionModel().getSelectedNode();
		    	if(!node || !node.isLeaf()) {
		    		Ext.Msg.show({
   						title:"提示",
   						msg:"未选择具体卡产品，请先选择卡产品!",
   						buttons:Ext.Msg.OK,
   						icon: Ext.MessageBox.WARNING
					});
					return;
		    	}
	    		data_store.baseParams = data_store.baseParams || {};

	    		data_store.baseParams["paperCityCrd.businpnt_no"] = Ext.getCmp("a").getValue();
	    		data_store.baseParams["paperCityCrd.print_type"] = Ext.getCmp("b").getValue();
							
				data_store.load({params:{start:0,limit:thislimit}});
			};
			
			var p1 = {
				id : "p1",
				xtype : "grid",
				stripeRows: true,
				loadMask : true,
				autoExpandColumn: "type",
				autoExpandMin: 200,
				store : data_store,
				headersDisabled: true,
				columns:[
			    	new Ext.grid.RowNumberer({header: "序号", width: 50}),
			    	{header: "外包商", 	dataIndex: "businpnt_name", menuDisabled: true},
			    	{header: "发行城市",	dataIndex: "city_name", menuDisabled: true},
			    	{header: "打印方式",	dataIndex: "print_type", id: "type",menuDisabled: true,
				    	renderer: function(input){
							if("0" == input) return "滚筒";
							else if("1" == input) return "折叠";
						}
					}
				],
				bbar: new Ext.ux.PagingToolbar( {
					pageSize : thislimit,
					store : data_store
				}),
				buttonAlign : "center",
				buttons:[{
					text:"新增", 
					handler: function(){
						var panel = this.ownerCt.ownerCt.ownerCt;
						var node = Ext.getCmp("epibCityTree").getSelectionModel().getSelectedNode();
						Ext.getCmp("citypanel").getRootNode().removeAll();
						if(node) {
							if(node.isLeaf()) {
								Ext.getCmp("p2").resetParam(node.id,node.text);
								oper_type = "upd";
								panel.layout.setActiveItem(1);
							} else {
								Ext.Msg.show({
		   							title:"警告",
		   							msg: "请先选择对应卡产品，然后点击新增!",
		   							buttons: Ext.Msg.OK,
		   							animEl: "elId",
		   							icon:Ext.MessageBox.WARNING
								});
							}
						} else {
							Ext.Msg.show({
	   							title:"警告",
	   							msg: "请先选择对应卡产品，然后点击新增!",
	   							buttons: Ext.Msg.OK,
	   							animEl: "elId",
	   							icon:Ext.MessageBox.WARNING
							});
						}
					}
				},{
					text:"修改", 
					handler: function(){
						var p1 = this.ownerCt.ownerCt;
						var panel = p1.ownerCt;
						var record = p1.getSelectionModel().getSelected();
						if (record){
							Ext.getCmp("p2").setParam(record.data);
							oper_type = "upd";
							panel.layout.setActiveItem(1);
						} else {
							Ext.Msg.show({
	   							title:"警告",
	   							msg: "请先选择一条记录，然后点击修改!",
	   							buttons: Ext.Msg.OK,
	   							animEl: "elId",
	   							icon:Ext.MessageBox.WARNING
							});
							
						}	
					}
				}],
				tbar:["&nbsp&nbsp外包商:",{
					id: "a",
					xtype: "billCombox",
					displayField: "name",
					width: 120,
					enableKeyEvents: true,
					store: epiboly_store1,
					listeners: {
						"select": sel
					}
				},"&nbsp&nbsp打印方式:",{
					id: "b",
					xtype: "billCombox",
					width: 120,
					value: "",
					store: print_store1,
					listeners: {
						"select": sel
					}
				}]
			};

			var p2 = {
				id:"p2",
				region: "north",
				border:false,
				xtype :"form",
				title: "外包商作业分配",
				height:125,
				bodyStyle : "padding:5px 5px 0",
				defaultType : "textfield",
				items: [{
					id: "paperCityCrd.card_no", 
					anchor: "100%",
					hidden:true, 
					name: "paperCityCrd.card_no"
				},{
					id: "paperCityCrd.card_name", 
					fieldLabel: "卡产品名称",	
					name: "paperCityCrd.card_name",
					anchor: "100%"
				},{
					id: "paperCityCrd.businpnt_no",
					fieldLabel: "外包商名称",
					allowBlank: false,
					name: "paperCityCrd.businpnt_no",
					xtype: "billCombox",
					store: epiboly_store,
					valueField: "id",
					displayField: "name",
					listeners: {
						"select": function(){
							if(!Ext.getCmp("p2").getForm().isValid()){
								Ext.Msg.show({
		   							title:"警告",
		   							msg: "验证不通过!",
		   							buttons: Ext.Msg.OK,
		   							animEl: "elId",
		   							icon:Ext.MessageBox.WARNING
									});
								return;
							}
							var businpntno = this.value;
							var prodno = Ext.getCmp("paperCityCrd.card_no").getValue();
							Ext.Ajax.request({
								params: {
									"paperCityCrd.card_no": prodno,
									"paperCityCrd.businpnt_no": businpntno
						   	   	},
							   	url: "treeCheck!epibCity.action",
							   	success: function(res){
									var rootnode=Ext.getCmp("citypanel").getRootNode();
									rootnode.removeAll();
									rootnode.appendChild(eval("("+res.responseText+")"));
							   	}
							});
						}
					}
				},{
					id: "paperCityCrd.print_type", 
					fieldLabel: "打印方式",	
					name: "paperCityCrd.print_type",
					xtype: "billCombox",
					store: print_store,
					allowBlank: false,
					value: "0",
					anchor: "100%"
				}],
				setParam: function(data){
				var p2 = this;
				Ext.getCmp("paperCityCrd.card_no").setValue(data.card_no);
				Ext.getCmp("paperCityCrd.card_name").setValue(data.card_name);
				Ext.getCmp("paperCityCrd.card_name").setDisabled(true);
				Ext.getCmp("paperCityCrd.businpnt_no").setValue(data.businpnt_no);
				Ext.getCmp("paperCityCrd.print_type").setValue(data.print_type);
				
				Ext.Ajax.request({
					params: {
						"paperCityCrd.card_no": data.card_no,
						"paperCityCrd.businpnt_no": data.businpnt_no
			   	   	},
				   	url: "treeCheck!epibCity.action",
				   	success: function(res){
						var rootnode=Ext.getCmp("citypanel").getRootNode();
						rootnode.removeAll();
						rootnode.appendChild(eval("("+res.responseText+")"));
				   	}
				});
			},
			resetParam: function(id, name){
				Ext.getCmp("paperCityCrd.card_no").setValue(id);
				Ext.getCmp("paperCityCrd.card_name").setValue(name);
				Ext.getCmp("paperCityCrd.card_name").setDisabled(true);
				Ext.getCmp("paperCityCrd.businpnt_no").setValue("");
				Ext.getCmp("paperCityCrd.print_type").setValue("0");
			}
			};
			
			var p3=new Ext.Panel({
				layout:"border",
				items:[p2,{
					region:"center",
					xtype: "billTreePanel",
				    id:"citypanel",
					autoScroll:true,
					title: "发行城市设置",
					rootVisible:false,
					tbar:[{
						xtype:"checkbox",
						boxLabel:"全选",
						listeners:{
							"check":function(t,checked){
								var nodes=t.ownerCt.ownerCt.getRootNode().childNodes;
								for(var n in nodes){
									nn=nodes[n].ui;
									if(nn){
										nn.toggleCheck(checked);
									}
								}
							}
						}
					}],
					root:{
						id:"root"
					}
				}],
				buttonAlign : "center",
				buttons:[{text:"保存",id:"save", handler: function(e){
					var form = Ext.getCmp("p2").getForm();
					var prodno = Ext.getCmp("paperCityCrd.card_no").getValue();
					var epibolyno = Ext.getCmp("paperCityCrd.businpnt_no").getValue();
					var printtype = Ext.getCmp("paperCityCrd.print_type").getValue();
					if("" == epibolyno){
						Ext.Msg.show({
   							title:"警告",
   							msg: "请先选择外包商，然后选择对应发行城市!",
   							buttons: Ext.Msg.OK,
   							animEl: "elId",
   							icon:Ext.MessageBox.WARNING
						});
						return false;
					}
					
					e.setDisabled(true);
					Ext.getCmp("aaa").layout.setActiveItem(0);
					Ext.getCmp("citypanel").root.expandChildNodes();
					var t_citys=Ext.getCmp("citypanel").getChecked();
					var citys="";
					for(var i=0;i<t_citys.length;i++){
						if(t_citys[i].isLeaf())
							citys+="|"+t_citys[i].attributes.id;
					}
					citys=citys.substring(1);
					var url,params;
					if(oper_type == "add") {
						url="epibCity!add.action";
					} else if(oper_type == "upd") {
						url="epibCity!upd.action";
					}
					params = {
						"paperCityCrd.card_no": prodno,
						"paperCityCrd.businpnt_no": epibolyno,
						"paperCityCrd.city_no": citys,
						"paperCityCrd.print_type": printtype
					};
					params2 = {
						"paperCityCrd.card_no": prodno,
						"paperCityCrd.businpnt_no": epibolyno
					};					
					oper(url,params,params2);
				}},{text:"取消", handler: function(){
					Ext.getCmp("aaa").layout.setActiveItem(0);
				}
			}]
		});

		var epicity_pel = new Ext.Panel( {
				id:"master_panel",
				border : false, 
				layout: "border",
				items:[{
					id:"epibCityTree",
					xtype : "billTreePanel",
					split:false,
					dataUrl : "tree!product.action",
					root : new Ext.tree.AsyncTreeNode( {
						id : "root"
					// 自动展开节点
					}),
					listeners : {
						"click"	: function(node) {
							tree_store.load({params:{node:node.attributes.id}});
							if(node.isLeaf()) {
								Ext.getCmp("aaa").layout.setActiveItem(0);
								data_store.baseParams = data_store.baseParams || {};
								data_store.baseParams["paperCityCrd.card_no"] = node.attributes.id;
								data_store.load({params:{start : 0,limit :thislimit}});
							}
						}
					}
				}, {
					id:"aaa",
					region: "center",
					border : false, 
					layout:"card",
					activeItem:0,//显示第一个容器
					items: [p1, p3]
				} ]
			});
			epiboly_store.load();
			epiboly_store1.load({callback: function(){
				Ext.getCmp("a").setValue("");
			}});
			
	
	var CenterPanel=Ext.getCmp("CenterPanel");
	CenterPanel.removeAll();
	CenterPanel.add(epicity_pel);
	CenterPanel.doLayout();
	</script>
    
   
	<div id="epibcity_manager">
	</div>
